package com.hunttown.mes.convert;

import com.google.common.collect.Lists;
import com.hunttown.mes.common.utils.BeanUtils;
import com.hunttown.mes.domain.TransportExpenseDetail;
import com.hunttown.mes.rpc.domain.TransportExpenseDetailDTO;
import com.hunttown.mes.rpc.exception.ConvertException;
import com.hunttown.mes.rpc.domain.enums.ErrorCode;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;
import java.util.List;

/**
 * created by wangjunfu on 2019-07-07 10:50:29
 */
public class TransportExpenseDetailConverts {

    private static Logger LOG = LoggerFactory.getLogger(TransportExpenseDetailConverts.class);

    public static List<TransportExpenseDetailDTO> convert(List<TransportExpenseDetail> transportExpenseDetailList) {
        if (CollectionUtils.isEmpty(transportExpenseDetailList)) {
            return Collections.EMPTY_LIST;
        }

        List<TransportExpenseDetailDTO> transportExpenseDetailListResult = Lists.newArrayList();
        for (TransportExpenseDetail transportExpenseDetail : transportExpenseDetailList) {
            transportExpenseDetailListResult.add(convert(transportExpenseDetail));
        }
        return transportExpenseDetailListResult;
    }

    public static TransportExpenseDetailDTO convert(TransportExpenseDetail transportExpenseDetail) {
        try {
            return BeanUtils.copy(transportExpenseDetail, TransportExpenseDetailDTO.class);
        } catch (Exception e) {
            LOG.error("TransportExpenseDetail到TransportExpenseDetailDTO转换失败!", e);
            throw new ConvertException(ErrorCode.CONVERT_ERROR, "TransportExpenseDetail到TransportExpenseDetailDTO转换失败", e);
        }
    }

    public static TransportExpenseDetail convert(TransportExpenseDetailDTO transportExpenseDetailDTO) {
        try {
            return BeanUtils.copy(transportExpenseDetailDTO, TransportExpenseDetail.class);
        } catch (Exception e) {
            LOG.error("TransportExpenseDetailDTO到TransportExpenseDetail转换失败!", e);
            throw new ConvertException(ErrorCode.CONVERT_ERROR, "TransportExpenseDetailDTO到TransportExpenseDetail转换失败", e);
        }
    }

    public static List<TransportExpenseDetail> converto(List<TransportExpenseDetailDTO> transportExpenseDetailDTOS) {
        if (CollectionUtils.isEmpty(transportExpenseDetailDTOS)) {
            return Collections.EMPTY_LIST;
        }

        List<TransportExpenseDetail> transportExpenseDetails = Lists.newArrayList();
        for (TransportExpenseDetailDTO transportExpenseDetailDTO : transportExpenseDetailDTOS) {
            transportExpenseDetails.add(convert(transportExpenseDetailDTO));
        }
        return transportExpenseDetails;
    }
}